package com.site.filter;

import javax.servlet.*;
import java.io.IOException;

/*字符过滤器*/
public class CharsetFilter implements Filter {
    private String charset  = null;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("字符过滤器开启");
        charset=filterConfig.getInitParameter("charset") == null?filterConfig.getInitParameter("charset"):"UTF-8";
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(charset);
        filterChain.doFilter(servletRequest,servletResponse);//放行
    }

    @Override
    public void destroy() {
        System.out.println("字符过滤器销毁");
    }
}
